<?php
/**
 * Created by PhpStorm.
 * User: HS032
 * Date: 2020/12/4
 * Time: 14:15
 */

namespace ticket\modules\backend\controllers;


use Yii;
use ticket\libs\Curl;
use ticket\libs\Tools;
use ticket\modules\backend\models\DqTicketList;

class DqTicketController extends BasicController
{
    // 获取门票接口地址
    protected  $url = 'https://jqxcx.klygf.com/dq_ticket_inter/inteface/routine/productList.json';

    /*** 门票列表
     * @return false|string
     */
    public function actionTicketList()
    {
        $get = Yii::$app->request->get();
        $page = isset($get['page'])?$get['page']:1;
        $pageSize = isset($get['size'])?$get['size']:10;
        $serarchModel = DqTicketList::find();
        $serarchModel->filterWhere(['like','name',$get['name']]);
        $count = $serarchModel->count();
        $ticketList = $serarchModel->offset($pageSize * ($page - 1))->orderBy(['id' => SORT_DESC])->limit($pageSize)->asArray()->all();
        
        return Tools::showRes(0,'success',[
            'ticketList' => $ticketList,
            'page' => $page,
            'size' => $pageSize,
            'count' => $count,
        ]);
    }

    /*** 获取恐龙园门票 保存
     * @return false|string
     * @throws \yii\db\Exception
     */
    public function actionObtainTicket()
    {

        $ticketList = $this->getKlyTicket();
        if ($ticketList and is_array($ticketList)){
//            $sql = "truncate dq_ticket_list";
//            \Yii::$app->db->createCommand($sql)->execute();
            $ok = 0;
            foreach ($ticketList as $key => $val){
                $newData = [
                    'k_id' => $val['id'],
                    'price' => $val['price'],
                    'name' => $val['name'],
                    'status' => 2,
                    'create_time' => date("Y-m-d H:i:s"),
                ];

                $info = Yii::$app->db->createCommand()->insert('dq_ticket_list', $newData)->execute();
                if ($info) {
                    $ok++;
                }
            }
            return Tools::showRes(0,'获取了'.$ok.'条门票记录');
        }
        return Tools::showRes(300,'获取失败--'.$ticketList['message']);

    }


    public function actionSetTicket()
    {
        if (\Yii::$app->request->isPost){
            $post = \Yii::$app->request->post();
            $dqModel = new DqTicketList();
            if ($dqModel->setInfo($post)){
                return Tools::showRes(0,'success');
            }
            return Tools::showRes(300,'修改失败');
        }
    }


    public function actionGetTicketInfo()
    {
        if (\Yii::$app->request->isPost){
            $post = \Yii::$app->request->post();
            $dqModel = new DqTicketList();
            $info = $dqModel->getInfo($post);
            return Tools::showRes(0,'success',$info);
        }
    }

    public function actionDelInfo()
    {
        if (\Yii::$app->request->isPost){
            $post = \Yii::$app->request->post();
            $dqModel = new DqTicketList();
            if ($dqModel->delInfo($post)){
                return Tools::showRes(0,'删除成功');
            }
            return Tools::showRes(300,'删除失败');
        }
    }
    

    public function getKlyTicket()
    {
        $url = $this->url;
        $list = json_decode(Curl::post($url,''),true);
        if ($list['code'] == '00'){
            return $list['data'];
        }
        return $list;
    }
    
    
    
    
    
}